package com.alibaba.algo.core.base;


import com.alibaba.algo.core.base.Order;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: glorysongglory
 * Date: 2020/5/9
 * Time: 10:04
 */
public class CoreTools {
    /**
     * 通过实体名称转换数据库字段名称
     */
    public static String fieldToColumn(String fieldName) {
        StringBuffer sb = new StringBuffer();
        for (int x = 0; x < fieldName.length(); x++) {
            Character c = fieldName.charAt(x);
            if (Character.isUpperCase(c)) {
                sb.append("_");
            }
            sb.append(c);
        }
        return sb.toString().toUpperCase();
    }

    public static String getSortStr(LinkedHashMap<String, Object> sortMap) {
        String sort = null;
        if (null != sortMap && null != sortMap.entrySet() && sortMap.entrySet().size() > 0) {
            StringBuffer sb = new StringBuffer();
            for (Map.Entry<String, Object> entry : sortMap.entrySet()) {
                sb.append(fieldToColumn(entry.getKey()));
                sb.append(" ");
                sb.append(Order.forCode(entry.getValue().toString()));
                sb.append(",");
            }
            String colStr = sb.toString();
            sort = colStr.substring(0, colStr.length() - 1);
        }
        return sort;
    }

}
